package com.sdp.refund.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.annotation.OperLog;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.refund.db.refund_user.entity.RefundUser;
import com.sdp.refund.db.refund_user.service.IRefundUserService;
import com.sdp.refund.utils.RefundConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/RefundUserController")
public class RefundUserController {

	@Autowired
	private IRefundUserService iRefundUserService;
	

	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iRefundUserService, RefundUser.class, dataCenter));
	}
	
	/**
	 * 添加用户信息
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody RefundUser user) {
		if(user==null) {
			return Result.error("用户信息为空");
		}
		if(StringUtils.isEmpty(user.getKeyId())) {
			if(RefundConstants.数据类型_退费.getCode().equals(user.getDataType())) {
				user.setRefundState(RefundConstants.退费状态_未申请.getCode());
			}
			user.setCreateTime(LocalDateTime.now());
			iRefundUserService.save(user);
		}else {
			iRefundUserService.updateById(user);
		}
		return Result.ok();
	}
	
	/**
	 * 退费受理，H5
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/approve")
	@Transactional(rollbackFor=Exception.class)
	@OperLog(operSystemNo="refund",operDesc="退费申请受理")
	public Result<?> approve(@RequestBody RefundUser user) {
		if(user==null || StringUtils.isEmpty(user.getKeyId())) {
			return Result.error("信息为空");
		}
		
		RefundUser dbUser = iRefundUserService.getById(user.getKeyId());
		if(dbUser==null) {
			return Result.error("退费信息不存在，请确认");
		}
		
		if(!RefundConstants.退费状态_已申请.getCode().equals(dbUser.getRefundState())) {
			return Result.error("不是申请状态，无法受理，请先确认");
		}
		if(RefundConstants.退费状态_已受理.getCode().equals(user.getRefundState())) {
			user.setRejectCause("");
		}
		user.setAcceptTime(LocalDateTime.now());
		iRefundUserService.updateById(user);
		return Result.ok();
	}
	
	/**
	 * 窗口受理
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/windowaApprove")
	@Transactional(rollbackFor=Exception.class)
	@OperLog(operSystemNo="refund",operDesc="窗口退费受理")
	public Result<?> windowaApprove(@RequestBody RefundUser user) {
		if(user==null || StringUtils.isEmpty(user.getKeyId())) {
			return Result.error("信息为空");
		}
		
		RefundUser dbUser = iRefundUserService.getById(user.getKeyId());
		if(dbUser==null) {
			return Result.error("退费信息不存在，请确认");
		}
		
		if(RefundConstants.退费状态_已受理.getCode().equals(dbUser.getRefundState())
				|| RefundConstants.退费状态_已退费.getCode().equals(dbUser.getRefundState())) {
			return Result.error("此信息已受理或者已退款，无法再次受理，请先确认");
		}
		if(RefundConstants.退费状态_已受理.getCode().equals(user.getRefundState())) {
			user.setRejectCause("");
		}
		user.setAcceptTime(LocalDateTime.now());
		iRefundUserService.updateById(user);
		return Result.ok();
	}
	
	/**
	 * 退款
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/refund")
	@Transactional(rollbackFor=Exception.class)
	@OperLog(operSystemNo="refund",operDesc="财务退款")
	public Result<?> refund(@RequestBody RefundUser user) {
		if(user==null || StringUtils.isEmpty(user.getKeyId())) {
			return Result.error("信息为空");
		}
		
		RefundUser dbUser = iRefundUserService.getById(user.getKeyId());
		if(dbUser==null) {
			return Result.error("受理的退费信息不存在，请确认");
		}
		
		if(!RefundConstants.退费状态_已受理.getCode().equals(dbUser.getRefundState())) {
			return Result.error("不是已受理状态，无法退款，请先确认");
		}
		user.setRefundTime(LocalDateTime.now());
		user.setRefundState(RefundConstants.退费状态_已退费.getCode());
		iRefundUserService.updateById(user);
		return Result.ok();
	}
	
}

